package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(this.brandService.queryBrandByIds(ids));
    }

    @GetMapping("/page")
    public ResponseEntity<PageDTO> queryBrandPage(
            @RequestParam(value = "key",required = false)String key
            ,@RequestParam(value = "page",defaultValue = "1")Integer page
            ,@RequestParam(value = "rows",defaultValue = "5")Integer rows){
        return ResponseEntity.ok(this.brandService.queryBrandPage(key,page,rows));
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.brandService.queryBrandByCategory(id));
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(@RequestBody BrandDTO brandDTO){
        this.brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(@RequestBody BrandDTO brandDTO){
        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id")Long id){
        this.brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
